package com.nineclock.message.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NCException;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.dto.SysAllowedJoinCompanyUserDTO;
import com.nineclock.message.config.JPushManager;
import com.nineclock.message.dto.NcMessageDto;
import com.nineclock.message.dto.NcMessageQueryDTO;
import com.nineclock.message.enums.MessageTypeEnum;
import com.nineclock.message.pojo.NcMessage;
import com.nineclock.message.service.NcMessageService;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@Slf4j
public class NcMessageServiceImpl implements NcMessageService {
    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    JPushManager jPushManager;
    @Override
    public void pushMessage(NcMessage ncMessage) {
        // 封装message属性,动态获取发送的标题
        String title = MessageTypeEnum.getEnumByType(ncMessage.getMessageType()).getTitle();
        // 获取以什么形式推送的
        String audience = MessageTypeEnum.getEnumByType(ncMessage.getMessageType()).getAudience();
        ncMessage.setTitle(title);
        ncMessage.setAudience(audience);
        // 存到mongoDB中
        mongoTemplate.save(ncMessage);
        // 推送消息
        try {
            log.info("准备推送的消息" + ncMessage);
            String content = ncMessage.getContent();
            List<String> targets = ncMessage.getTargets();
            jPushManager.sendNotificationByAlias(title, content, targets, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取文档推送历史记录,从mongodb中获取
     * @param ncMessageQueryDTO
     * @return
     */
    @Override
    public List<NcMessageDto> findMessageHistory(NcMessageQueryDTO ncMessageQueryDTO) {
        if (ncMessageQueryDTO == null) {
            ncMessageQueryDTO = new NcMessageQueryDTO();
        }
        // 组装查询条件
        Query query = new Query();
        if (StrUtil.isNotEmpty(ncMessageQueryDTO.getId())) {
            //query.addCriteria(Criteria.where("_id").is(new ObjectId(ncMessageQueryDTO.getId())));
            query.addCriteria(Criteria.where("_id").is(ncMessageQueryDTO.getId()));
        }
        if (StrUtil.isNotEmpty(ncMessageQueryDTO.getType())) {
            query.addCriteria(Criteria.where("messageType").is(ncMessageQueryDTO.getType()));
        }
        if (ncMessageQueryDTO.getUserId()!=null) {
            query.addCriteria(Criteria.where("applyUserId").is(ncMessageQueryDTO.getUserId()));
        }
        if (ncMessageQueryDTO.getTargetId()!=null) {
            query.addCriteria(Criteria.where("approveUserId").is(ncMessageQueryDTO.getTargetId()));
        }

        query.with(Sort.by(Sort.Direction.ASC, "approveStatue"));

        List<NcMessage> list = mongoTemplate.find(query, NcMessage.class);
        List<NcMessageDto> ncMessageDtos = BeanHelper.copyWithCollection(list, NcMessageDto.class);
        return ncMessageDtos;

    }

    /**
     * 更新mongo中的状态
     * @param id
     */
    @Override
    public void allowedMessageUpdate(String id) {
        // 根据条件修改MongoDB中的状态
        Query query = new Query();
        if (StrUtil.isNotEmpty(id)) {
            query.addCriteria(Criteria.where("_id").is(new ObjectId(id)));
        }
        Update update = new Update();
        update.set("approveStatue", 1);
        update.set("useStatus", 1);
        mongoTemplate.updateFirst(query, update, NcMessageDto.class);
    }
}
